package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.service.service.belt.IBeltDayTargetHisService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltDayTargetHisReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltDayTargetHisRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltDayTargetHisAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltDayTargetHis;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltDayTargetHisMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实现类
 * @author 林柏云
 * @date 2024-12-12
 */
@Slf4j
@Service
public class BeltDayTargetHisServiceImpl extends ServiceImpl<BeltDayTargetHisMapper, BeltDayTargetHis> implements IBeltDayTargetHisService {

  @Autowired
  private BeltDayTargetHisMapper baseMapper;

  @Override
  public BeltDayTargetHisRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltDayTargetHis entity = baseMapper.selectById(id);
    return BeltDayTargetHisAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltDayTargetHisRespVO> page(PageRequest<BeltDayTargetHisReqDTO> request) {
    BeltDayTargetHisReqDTO condition = request.getQueryCondition();
    Page<BeltDayTargetHisRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltDayTargetHisRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltDayTargetHisRespVO> list(BeltDayTargetHisReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltDayTargetHisReqDTO reqDto) {
    BeltDayTargetHis entity = BeltDayTargetHisAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltDayTargetHisReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltDayTargetHisReqDTO reqDto) {
    BeltDayTargetHis oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltDayTargetHis newEntity = BeltDayTargetHisAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltDayTargetHis oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }
  @Override
  public BeltDayTargetHis selectLatest() {
    return baseMapper.selectLatest();

  }
}